﻿
using System ;

using MskEdit.Dxf.Model ;


namespace MskEdit.Dxf.Backend
{
    public static class DxfSectionReader
    {

        internal static DxfSection TryRead( string[] allLines, ref int lineNo, DxfSection fallbackSection ) {
        
            int internalLineNo = lineNo ;            

            if ( DxfLineReader.TryRead( "0", "SECTION", allLines, ref internalLineNo, null ) == null ) 
                return fallbackSection ;
                
            DxfSection dxfSection = DxfSection.TryFrom( allLines, ref internalLineNo, null ) ;
            if (dxfSection == null )
                return fallbackSection ;
            
            do {
/*            
                DxfHeader dxfHeader = DxfHeaderReader.TryRead( allLines, ref internalLineNo, null ) ;
                if ( dxfHeader != null ) {
//                    dxfSection.Header = dxfHeader ;
                    break ;
                }
            
                DxfClasses dxfClasses = DxfClassesReader.TryRead( allLines, ref internalLineNo, null ) ;
                if ( dxfClasses != null ) {
                    dxfSection.Classes = dxfClasses ;
                    break ;
                }
            
                DxfTables dxfTables = DxfSymbolTablesReader.TryRead( allLines, ref internalLineNo, null ) ;
                if ( dxfTables != null ) {
                    dxfSection.Tables = dxfTables ;
                    break ;
                }
*/                
            } while ( false ) ;

                    
            if ( DxfLineReader.TryRead( "0", "ENDSEC", allLines, ref internalLineNo, null ) == null ) {
                string lineNoStr = ( internalLineNo + 1 ).ToString() ;
                string msg = "### Warning: \"ENDSEC\" not found at line " + lineNoStr +"." ;
                System.Diagnostics.Trace.WriteLine( msg ) ;
            }

            lineNo = internalLineNo ;
            
            return dxfSection ;
        }
    }
}
